package com.cls.business.service.impl;

import com.cls.business.entity.DeclareTopic;
import com.cls.business.vo.DeclareTopicVO;
import com.cls.common.entity.QueryRequest;
import com.cls.business.entity.Report;
import com.cls.business.mapper.ReportMapper;
import com.cls.business.service.IReportService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

/**
 * 举报表 Service实现
 *
 * @author wmm
 * @date 2020-11-23 10:49:10
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {

    @Override
    public PageInfo<Report> findReports(QueryRequest request, Report report) {
        LambdaQueryWrapper<Report> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Report> list = this.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<Report> findReports(Report report) {
        LambdaQueryWrapper<Report> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        if(report.getExpertId()!=null){
            queryWrapper.eq(Report::getExpertId,report.getExpertId());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createReport(Report report) {
        this.save(report);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateReport(Report report) {
        this.saveOrUpdate(report);
    }


    /**
     * 删除
     *
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteReports(String[] ids) {
        List<String> list = Arrays.asList(ids);
        this.removeByIds(list);
    }

    @Override
    public PageInfo<DeclareTopicVO> findDeclareTopicsForReport(QueryRequest request, DeclareTopic declareTopic) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<DeclareTopicVO> declareTopics = this.baseMapper.findDeclareTopics(declareTopic);
        PageInfo pageInfo = new PageInfo(declareTopics);
        return pageInfo;
    }

    @Override
    public DeclareTopicVO findDetail2Report(Long reportId) {
        return this.baseMapper.findDetail2Report(reportId);
    }

}
